<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;

/**
* 公司管理
*/
class Company extends AdminBase
{
	
	protected function _initialize()
	{
		parent::_initialize();
        //获取列表导航信息,2为单页
        // $class_type = db('nav')->where(['status'=>1])->order(['sort' => 'DESC', 'id' => 'ASC'])->column('name','id');
        // $this->assign('class_type', $class_type);
	}
	public function index(){
		$list = db('company')->order('sort desc,add_time desc')->paginate(15);
		return $this->fetch('index',['list'=>$list]);
	}
	public function add(){
		return $this->fetch('add');
	}
	public function save(){
		if($this->request->isPost()){
			$data = $this->request->except('content','post');//排除content，接收post值
			$data['content'] = $_POST['content'];
        	//默认上传图片
        	if (empty($data['img_url'])) {
            	$data['img_url'] = "/public/static/index/images/news_2.jpg";
        	}

			if(db('company')->insert($data) !== false){
				$this->success('添加成功','index');
			}else{
				$this->error('提交失败');
			}
		}
	}
	public function edit($id){
		return $this->fetch('edit',['rows'=>db('company')->find($id)]);
	}
	public function update(){
 		if($this->request->isPost()) {
            $data = $this->request->except('content', 'post');
            $data['content'] = $_POST['content'];
            // 如果没有摘要;则截取文章内容的前120字作为描述
            if (db('company')->update($data) !== false) {
                $this->success('更新成功', 'index');
            } else {
                $this->error('更新失败');
            }
        }
	}
	/*
	 * 更新'0'或'1'
	 */
	public function flag($id){
        if (request()->isPost()) {
            $data = input('post.');
            if (db('company')->where(['id' => $id])->update($data) !== false) {
                $this->success('更新成功');
            } else {
                $this->error('更新失败');
            }
        }
    }
    public function delete($id = 0, $ids = [])
    {
        $id = $ids ? $ids : $id;
        if ($id) {
            if (db('job')->where('cid',$id)->find()) {
                $this->error('该公司下有职位还未删除！');
            }
            if (db('company')->delete($id)) {
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        } else {
            $this->error('请选择需要删除的项目');
        }
    }
}